/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.UserManagement;
import jard.webshop.nbp.User;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author CJP
 */
@ManagedBean(name = "loginBackingBean")
@RequestScoped
public class LoginBackingBean {
    
    private UserManagement um = null;
    @ManagedProperty(value = "#{userModelBean}")
    private UserModelBean user;
    
    private String username = "";
    private String password = "";

    /** Creates a new instance of LoginBackingBean */
    public LoginBackingBean() {
        um = UserManagement.getInstance();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public String doLogin() {
        System.out.println("Doing login");
        User fetchedUser = um.getUser(username, password);
        if(fetchedUser == null) {
            System.out.println("Failed!");
            return "failed";
        } else {
            System.out.println("Success!");
            user.setUser(fetchedUser);
            return "success";
        }
    }
    
    public String doLogout() {
            user.setUser(null);
            return "main";
    }

    public UserModelBean getUser() {
        return user;
    }

    public void setUser(UserModelBean user) {
        this.user = user;
    }
    
}
